class RelationshipsController < ApplicationController
  before_action :set_post, only: [:create, :destroy]
  before_action :set_relationship,  only: [ :destroy]
  def destroy
    @relationship.delete
    flash[:success] = "删除成功"
    redirect_to request.referrer || posts_path
  end

  def create
    @relationship = Relationship.create(relationship_params)
    @relationship.post_id = params[:post_id]
    respond_to do |format|
      if @relationship.save
        format.html { redirect_to  post_path(@post), notice: '添加品论成功' }
        format.json { render :index, status: :created, location: @user }
      else
        flash[:error] = '评论内容为空'
        format.html { redirect_to   post_path(@post) }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
  private
  def set_post
    @post = Post.find(params[:post_id])
  end

  def set_relationship
    @relationship = Relationship.find(params[:id])
  end

  def relationship_params
    params.require(:relationship).permit(:comment_id, :post_id, :comment)
  end


end
